package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.registration.RegService;
import matlap.client.services.registration.RegServiceAsync;
import matlap.client.services.student.StudentService;
import matlap.client.services.student.StudentServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PasswordComposite extends Composite {
	StudentServiceAsync s = GWT.create(StudentService.class);
	RegServiceAsync r = GWT.create(RegService.class);

	private VerticalPanel vp = new VerticalPanel();

	public PasswordComposite() {
		initWidget(vp);
		vp.setSize("697px", "463px");
		initPanel();
	}

	private void initPanel() {
		FlexTable fl3 = new FlexTable();
		
		Label lblNewLabel = new Label("Jelsz\u00F3 v\u00E1ltoztat\u00E1sa. Alap\u00E9rtelmezett jelsz\u00F3: password");
		lblNewLabel.setStyleName("BigBold");
		fl3.setWidget(0, 0, lblNewLabel);
		vp.add(fl3);
		Label who2Label = new Label("Kinek a jelszav\u00E1t akarja m\u00F3dositani?");
		fl3.setWidget(3, 0, who2Label);
		who2Label.setWidth("225px");

		final ListBox who2comboBox = new ListBox();
		fl3.setWidget(4, 0, who2comboBox);

		who2comboBox.addItem("");

		r.getRegNames(new AsyncCallback<List<String>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					who2comboBox.addItem((String) it.next());
				}
			}

		});
//<<<<<<< .mine
//		fl3.getCellFormatter().setHorizontalAlignment(1, 3,
//				HasHorizontalAlignment.ALIGN_RIGHT);
//=======
				fl3.getCellFormatter().setHorizontalAlignment(6, 0, HasHorizontalAlignment.ALIGN_CENTER);
				fl3.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);
				fl3.getCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);
				
						Button btnSave = new Button("Ment\u00E9s");
						fl3.setWidget(6, 0, btnSave);
//>>>>>>> .r175
		
//				Button btnSave = new Button("Ment\u00E9s");
//				fl3.setWidget(1, 3, btnSave);
		btnSave.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String who = who2comboBox.getItemText(who2comboBox
						.getSelectedIndex());
				who2comboBox.setSelectedIndex(0);
				String p = "password";
				r.updatePassword(who, p, new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(Void result) {
						Window.alert("Siekeres jelsz\u00F3 m\u00F3dos\u00EDt\u00E1ss!");
					}

				});

			}
		});
	}
}
